package com.listener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class Test {
	@org.junit.Test
	public void bbTest() {
		// 创建一个流程数据源的配置
		ProcessEngine processEngine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"diagrams/activiti.cfg.xml").buildProcessEngine();
		// 获取数据源连接的服务
		RepositoryService repositoryService = processEngine
				.getRepositoryService();
		// 读取配置流程配置文件对象
		Deployment deployment = repositoryService.createDeployment()
				.addClasspathResource("diagrams/MyProcess1.activiti.xml").deploy();
		// 发布到数据库中
		ProcessDefinition pdf = repositoryService
				.createProcessDefinitionQuery()
				.deploymentId(deployment.getId()).singleResult();

		// 开启一个服务
		RuntimeService runtimeService = processEngine.getRuntimeService();
            /*
             * 定义流程中的参数
             */
		// 建立一个集合存放流程中的参数
		Map<String, Object> model = new HashMap<String, Object>();
		// 开启流程的人
		model.put("user", "胖子");
		// 行政审核人
		model.put("manager", "肖正伟");
		// 审核结果
		model.put("backtrack", "yes");
		// 会签 （将所有参与会签的人放入list集合中）
		List<String> list = new ArrayList<String>();
		list.add("zhangsan");
		list.add("lisi");
		list.add("libo");
		list.add("xiao");
		list.add("haha");
		list.add("yunsi");
		// 把list结合放入集合中
		model.put("assigneeList", list);
		

		// 开启一个流程
		ProcessInstance pi = runtimeService.startProcessInstanceById(
				pdf.getId(), model);
		// 节点服务
		TaskService service = processEngine.getTaskService();
		// 获取节点
		
		
		/**
		 * 拟稿
		 */
		List<Task> tasks = service.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId())
				.taskAssignee("胖子").list();
		for (Task task : tasks) {

			service.complete(task.getId());
			System.out.println(task.getName());
		}

		// 行政审核节点
		tasks = service.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId())
				.taskAssignee("肖正伟").list();
		for (Task task : tasks) {
			service.complete(task.getId());
			System.out.println("行政部经理审核");
		}
		
		
		/**
		 * 
		 * 
		 * */
		
		tasks = service.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId())
				.taskAssignee("zhangsan").list();

		for (Task task2 : tasks) {
			service.complete(task2.getId());
			System.out.println(task2.getAssignee());
		}
/**
 * 
 * */
		tasks = service.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId())
				.taskAssignee("lisi").list();

		for (Task task : tasks) {
			service.complete(task.getId());
			System.out.println(task.getAssignee());
		}

		tasks = service.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId())
				.taskAssignee("libo").list();

		for (Task task : tasks) {
			service.complete(task.getId());
			System.out.println(task.getAssignee());
		}

		tasks = service.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId())
				.taskAssignee("xiao").list();

		for (Task task : tasks) {
			service.complete(task.getId());
			System.out.println(task.getAssignee());
		}
		tasks = service.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId())
				.taskAssignee("haha").list();

		for (Task task : tasks) {
			service.complete(task.getId());
			System.out.println(task.getAssignee());
		}
		tasks = service.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId())
				.taskAssignee("yunsi").list();

		for (Task task : tasks) {
			service.complete(task.getId());
			System.out.println(task.getAssignee());
		}
	}

}
